package mid

import (
	"fmt"
	"net/http"

	"gitee.com/firewing_group/model/comm"
	"gitee.com/firewing_group/model/ctxdata"
	"gitee.com/firewing_group/model/model/stuser"
	"gitee.com/firewing_group/test/someclient"
	"github.com/labstack/echo"
)

func UserAPILockMid(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		cc := c.Get("cc").(*ctxdata.Cusctx)
		user := ctxdata.GetUser(c).(*stuser.KuaiMaoUser)
		uri := c.Request().URL.RequestURI()
		cc.Logf("uri_%v", uri)
		encode := fmt.Sprintf("%v_%v", user.UserName, uri)
		apilock := someclient.CommonAPILockClient.LockEx(encode)
		defer someclient.CommonAPILockClient.UnlockEx(encode)
		cc.Logf("apilock:%v", apilock)
		if apilock {
			return next(c)
		}
		return c.JSON(http.StatusBadRequest, comm.Bad("thomesinglock", comm.STATUS_COMMON))
	}
}
